package com.github.core.parse.sse;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 大模型openai格式转化
 */
public class LlmParser {

    public static List<ChatResponse> parse(JSONObject resultObj, Boolean streamFlag, String streamField, Integer streamStep) {
        if(streamFlag) {
            String output = JSONUtil.getByPath(resultObj, streamField, "");
            return Arrays.stream(StrUtil.cut(output,streamStep))
                    .map(ChatResponse::build)
                    .collect(Collectors.toList());
        }else{
            String output = JSONUtil.getByPath(resultObj, streamField, "");
            return Collections.singletonList(ChatResponse.build2(output));
        }
    }
}
